using UnityEngine;

public class CloneSkill : MonoBehaviour
{
    private static readonly int AttackNumber = Animator.StringToHash("attackNumber");
    private Player player;
    private SpriteRenderer sr;
    private Animator anim;
    [SerializeField] private float colorLoosingSpeed;
    private float timer;
    private float attackMultiplier;
    private Transform attackCheck;
    private const string AttackCheckName = "AttackCheck";
    [SerializeField] private float attackCheckRadius;
    private Transform closestEnemyTf;
    private CloneSkillController cloneSkillController;
    private int facingDirection = 1;
    private bool canDuplicateClone;
    // 35% chance to duplicate
    private float chanceToDuplicate = 35f;

    private void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
        attackCheck = transform.Find(AttackCheckName);
        // player = FindAnyObjectByType<Player>();
    }

    private void Start() => cloneSkillController = FindAnyObjectByType<CloneSkillController>();

    private void Update()
    {
        timer -= Time.deltaTime;
        if (!(timer <= 0)) return;
        if (sr.color.a <= 0.1f)
        {
            Destroy(gameObject);
        }
        else
        {
            sr.color = new Color(1, 1, 1, sr.color.a - (Time.deltaTime * colorLoosingSpeed));
        }
    }

    public void Setup(Transform newTf, float cloneDuration, bool canAttack, Vector3 offset, Transform closestEnemyTf,
        bool canDuplicate, float chanceToDuplicate, Player player, float attackMultiplier)
    {
        if (canAttack)
        {
            int randomNumber = Random.Range(1, 4);
            anim.SetInteger(AttackNumber, randomNumber);
        }
        this.attackMultiplier = attackMultiplier;
        this.player = player;
        transform.position = newTf.position + offset;
        timer = cloneDuration;
        this.closestEnemyTf = closestEnemyTf;
        this.canDuplicateClone = canDuplicate;
        this.chanceToDuplicate = chanceToDuplicate;
        FaceClosestTarget();
    }

    private void FaceClosestTarget()
    {
        if (!closestEnemyTf) return;
        if (!(transform.position.x > closestEnemyTf.position.x)) return;
        facingDirection = -1;
        transform.Rotate(0, 180, 0);
    }

    private void AnimationTrigger() => timer = -0.1f;

    private void AttackTrigger()
    {
        Collider2D[] results = new Collider2D[10];
        var size = Physics2D.OverlapCircleNonAlloc(attackCheck.position, attackCheckRadius, results);
        for (int i = 0; i < size; i++)
        {
            if (!results[i].TryGetComponent(out Enemy enemy)) continue;
            enemy.SetupKnockbackDirection(transform.position);
            ((PlayerStats)player.Stats).CloneDoDamage(enemy.Stats, attackMultiplier);
            if (player.SkillManager.Clone.CanApplyOnHitEffect)
            {
                ItemDataEquipment weaponData = player.Inventory.GetEquipment(EquipmentType.Weapon);
                weaponData?.Effect(enemy.transform, player);
            }
            if (!canDuplicateClone) continue;
            if (Random.Range(0, 100) < chanceToDuplicate)
                cloneSkillController.Create(results[i].transform, new Vector3(1f * facingDirection, 0));
        }
    }
#if unity_editor
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        if (attackCheck == null) return;
        Gizmos.DrawWireSphere(attackCheck.position, attackCheckRadius);
    }
#endif
}